#include "MainFrame.h"
#include "ui_MainFrame.h"

#include <QRect>
#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>

MainFrame::MainFrame(QWidget *parent) :
    QFrame(parent)
    , ui(new Ui::MainFrame)
    , _currentContent(NULL)
    , _mainContent(NULL)
{
    ui->setupUi(this);
    InitMenuBar();

    _mainContent = new MainWindowFrame(this);
    ui->workspaceLayout->addWidget(_mainContent);
    this->resize(_mainContent->size());

    QRect screen = QApplication::desktop()->screenGeometry(this);
    if (screen.height() < 1000)
    {
#if 0
        QWidget* tab = ui->tabBottomRight->widget(0);
        ui->tabBottomRight->removeTab(0);
        ui->tabTopRight->addTab(tab, tr("Control"));
        tab = ui->tabBottomRight->widget(0);
        ui->tabBottomRight->removeTab(0);
        ui->tabTopRight->addTab(tab, tr("Chuck"));

        tab = ui->tabBottomRight->widget(0);
        ui->tabBottomRight->removeTab(0);
        ui->tabTopRight->addTab(tab, tr("Scope"));

        int index = ui->tabBottomRight->indexOf(ui->tabPositioner);
        if (PositionerContext::Context()->Count() > 0 && 0)
        {
            tab = ui->tabBottomRight->widget(index);
            ui->tabBottomRight->removeTab(index);
            ui->tabTopRight->addTab(tab, tr("Positioner"));
        }
        else
        {
            ui->tabBottomRight->removeTab(index);
        }

        tab = ui->tabBottomRight->widget(0);
        ui->tabBottomRight->removeTab(0);
        ui->tabTopRight->addTab(tab, tr("Blend"));
        ui->tabTopRight->setMaximumSize(QSize(5000, 5000));

        ui->tabBottomRight->setVisible(false);
    }
    else
    {
        int index = ui->tabBottomRight->indexOf(ui->tabPositioner);
        if (PositionerContext::Context()->Count() == 0 && 0)
        {
            ui->tabBottomRight->removeTab(index);
        }

        ui->tabBottomRight->setCurrentIndex(1);
#endif
    }
    InitStatusBar();
}

MainFrame::~MainFrame()
{
    delete ui;
}

void MainFrame::InitMenuBar()
{
    ui->menuBarDesign->hide();
    ui->menuFile->setMenu(ui->menuFileDesign);
    ui->menuTools->setMenu(ui->menuToolsDesign);
    ui->menuOptions->setMenu(ui->menuOptionsDesign);
    ui->menuWindow->setMenu(ui->menuWindowDesign);
    ui->menuHelp->setMenu(ui->menuHelpDesign);
}

//------------------------------------------------------------------------------------------------------
void MainFrame::InitStatusBar()
{
//    ui->lblStatusName->setStyleSheet("background-color: rgb(220, 220, 220);");
//    ui->lblStatusName->setText("Idle");
#if 0
    MagicAContext* context = McfContext::Current<MagicAContext>();
    Q_ASSERT(context != NULL);

    // Machine status.
    std::vector<StatusLabel::StatusLabelItem> statusItems;
    statusItems.push_back(StatusLabel::StatusLabelItem(MachineStatusIdle, tr("Idle"), QColor(220, 220, 220), Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(MachineStatusRunning, tr("Running"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(MachineStatusPaused, tr("Paused"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(MachineStatusError, tr("Error"), Qt::red, Qt::white));
    _machineStatus = new StatusLabel(QColor(160, 160, 160), statusItems, &context->MachineStatus, this);
    _machineStatus->setMinimumWidth(100);
    _machineStatus->setAlignment(Qt::AlignCenter);
    ui->statusLayout->insertWidget(1, _machineStatus);

    // Chuck position.
    statusItems.clear();
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosBelowScan, tr("Below Scan"), QColor(220, 220, 220), Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosScan, tr("Scan"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosScanSeparate, tr("Scan - Separate"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosSeparate, tr("Separate"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosSeparateBase, tr("Separate - Base"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosBase, tr("Base"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosBaseContact, tr("Base - Contact"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosScanBase, tr("Scan - Base"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosSafeBase, tr("Base"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosBaseSeparate, tr("Base - Separate"), Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosSeparateContact, tr("Separate - Contact"), Qt::yellow, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosContact, tr("Contact"), Qt::red, Qt::white));
    statusItems.push_back(StatusLabel::StatusLabelItem(ChuckPosAboveContact, tr("Above Contact"), Qt::red, Qt::white));
    _chuckStatus = new StatusLabel(QColor(160, 160, 160), statusItems, &context->MainContent()->ChuckPosition, this);
    _chuckStatus->setMinimumWidth(200);
    _chuckStatus->setAlignment(Qt::AlignCenter);
    ui->statusLayout->insertWidget(3, _chuckStatus);

    statusItems.clear();
    statusItems.push_back(StatusLabel::StatusLabelItem(MagicOnly, tr("Magic Only"), QColor(220, 220, 220), Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(NA, tr("NA"), QColor(220, 220, 220), Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(Online, "", Qt::green, Qt::black));
    statusItems.push_back(StatusLabel::StatusLabelItem(Offline, "", QColor(220, 220, 220), Qt::black));
    _testerStatus = new StatusLabel(QColor(160, 160, 160), statusItems, &context->MainContent()->TesterStatus, &context->MainContent()->TesterNameVersion, this);
    _testerStatus->setMinimumWidth(200);
    _testerStatus->setAlignment(Qt::AlignCenter);
    ui->statusLayout->insertWidget(5, _testerStatus);

    // Vacuum status.
    _vacuum = new IoButton(context->OutputSignal(DUMMY_DO_VAC), tr("Vacuum"), MCF_TRUE, this, MCF_FALSE);
    _vacuum->ControlWidget()->setProperty("headerLabel", true);
    ui->statusLayout->insertWidget(7, _vacuum);

    // TopCover status
    _topCover = new IoButton(context->InputSignal(DI_TOP_COVER_DOOR),tr("Top Cover Close"),MCF_TRUE,this,MCF_FALSE);
    _topCover->ControlWidget()->setProperty("headerLabel", true);
    ui->statusLayout->insertWidget(8,_topCover);


    // User display name label.
    ui->lblUser->setProperty("headerLabel", true);
    OnUserChanged(const_cast<User*>(&context->Identity()));
    connect(context, SIGNAL(UserChanged(User*)), this, SLOT(OnUserChanged(User*)));
    if (context->Scanner() != nullptr)
    {
        Magic2* magicWindow = Magic2::MainWindow();
        connect(context->MainContent().get(), SIGNAL(ChangeUser(QString, QString)), magicWindow, SLOT(OnChangeUserByScanner(QString, QString)));
    }
#endif
}
